/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Component;

import GameObject.Sprite;
import Global.ResourceManager;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;

/**
 *
 * @author Huy
 */
public class MyTextBox extends MyControlItem {

	private String text;
	private int cursorIndex;
	private int maxChar;

	public void setMaxChar(int maxChar) {
		this.maxChar = maxChar;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		cursorIndex = text.length();
		this.text = text;
	}

	public MyTextBox(BufferedImage img, String text, int x, int y) {
		sprite = new Sprite(img, 1, 2, x, y);
		this.text = text;
		cursorIndex = text.length();
		maxChar = 15;
	}

	public MyTextBox(String text, int x, int y) {
		sprite = new Sprite(ResourceManager.getImage("default/textbox"), 1, 2, x, y);
		this.text = text;
		cursorIndex = text.length();
		maxChar = 15;
	}
	int time = 0;

	@Override
	public void draw(Graphics2D g) {
		if (!isVisible) {
			return;
		}
		sprite.draw(g);
		if (isEnable) {
			g.setColor(foreColor);
		} else {
			g.setColor(disableColor);
		}

		Rectangle2D textBound = g.getFont().getStringBounds(text, g.getFontRenderContext());
		Rectangle2D selectTextBound = g.getFont().getStringBounds(text.substring(0, cursorIndex), g.getFontRenderContext());
		Rectangle spriteBound = sprite.getBound();
		int x = (int) (spriteBound.getCenterX() - textBound.getCenterX());
		int y = (int) (spriteBound.getCenterY() - textBound.getCenterY());
		g.drawString(text, x, y);

		if (isForcus) {
			if (time > 2) {
				drawCursor(g, (int) (x + selectTextBound.getWidth()),
						(int) (y - 2 * selectTextBound.getHeight() / 3), (int) (3 * selectTextBound.getHeight() / 4));
			}
			time = (time + 1) % 8;
		}
	}

	public void drawCursor(Graphics2D g, int x, int y, int l) {
		g.drawLine(x, y, x, y + l);
		g.drawLine(x - 2, y, x + 2, y);
		g.drawLine(x - 2, y + l, x + 2, y + l);
	}

	@Override
	public boolean keyPressed(KeyEvent e) {
		if (!super.keyPressed(e)) {
			return false;
		}

		switch (e.getKeyCode()) {
			case KeyEvent.VK_UP:
			case KeyEvent.VK_LEFT: {
				if (cursorIndex > 0) {
					cursorIndex--;
				}
				break;
			}
			case KeyEvent.VK_DOWN:
			case KeyEvent.VK_RIGHT: {
				if (cursorIndex < text.length()) {
					cursorIndex++;
				}
				break;
			}
			case KeyEvent.VK_BACK_SPACE: {
				if (cursorIndex > 0) {
					text = text.substring(0, cursorIndex - 1) + text.substring(cursorIndex);
					cursorIndex--;
				}
				break;
			}
			case KeyEvent.VK_DELETE: {
				if (cursorIndex < text.length()) {
					text = text.substring(0, cursorIndex) + text.substring(cursorIndex+1);
				}
				break;
			}
			//case KeyEvent.VK_ENTER:
			case KeyEvent.VK_ESCAPE: {
				lostForcus();
				break;
			}
			case KeyEvent.VK_SHIFT:
				break;
			default: {
				if (text.length() < maxChar) {
					char chr = e.getKeyChar();
					text = text.substring(0, cursorIndex) + chr + text.substring(cursorIndex);
					cursorIndex++;
				}
			}
		}
		return true;

	}

	@Override
	public boolean mouseMoved(MouseEvent e) {
		if (!super.mouseMoved(e)) {
			((Sprite) sprite).setFrame(0);
			return false;
		}
		((Sprite) sprite).setFrame(1);
		return true;
	}

	@Override
	public boolean mouseReleased(MouseEvent e) {
		if (!super.mouseReleased(e)) {
			return false;
		}
		//cursorIndex = text.length();
		return true;
	}
}
